package com.ktgj.customer.service.impl;

import java.io.File;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import cn.hutool.core.date.DateUtil;
import com.ktgj.common.core.constant.CustomerConstants;
import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.common.core.utils.ExcelUtils;
import com.ktgj.common.core.utils.HttpClientUtil;
import com.ktgj.customer.domain.Coupon;
import com.ktgj.customer.domain.Customer;
import com.ktgj.customer.domain.CustomerRightdetail;
import com.ktgj.customer.mapper.CouponMapper;
import com.ktgj.customer.mapper.CustomerMapper;
import com.ktgj.customer.mapper.CustomerRightdetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ktgj.customer.mapper.DdCouponUsedMapper;
import com.ktgj.customer.domain.DdCouponUsed;
import com.ktgj.customer.service.IDdCouponUsedService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-28
 */
@Service
@Transactional
public class DdCouponUsedServiceImpl implements IDdCouponUsedService 
{
    @Value("${ddCoupon.uploadFile}")
    private String ddCouponUsedPath;
    @Autowired
    private DdCouponUsedMapper ddCouponUsedMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private CustomerRightdetailMapper customerRightdetailMapper;


    /**
     * 查询【请填写功能名称】
     * 
     * @param couponUsedId 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public DdCouponUsed selectDdCouponUsedById(Long couponUsedId)
    {
        return ddCouponUsedMapper.selectDdCouponUsedById(couponUsedId);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param ddCouponUsed 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<DdCouponUsed> selectDdCouponUsedList(DdCouponUsed ddCouponUsed)
    {
        return ddCouponUsedMapper.selectDdCouponUsedList(ddCouponUsed);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param ddCouponUsed 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertDdCouponUsed(DdCouponUsed ddCouponUsed)
    {
        ddCouponUsed.setCreateTime(DateUtils.getNowDate());
        return ddCouponUsedMapper.insertDdCouponUsed(ddCouponUsed);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param ddCouponUsed 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateDdCouponUsed(DdCouponUsed ddCouponUsed)
    {
        return ddCouponUsedMapper.updateDdCouponUsed(ddCouponUsed);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param couponUsedIds 需要删除的【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteDdCouponUsedByIds(Long[] couponUsedIds)
    {
        return ddCouponUsedMapper.deleteDdCouponUsedByIds(couponUsedIds);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param couponUsedId 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteDdCouponUsedById(Long couponUsedId)
    {
        return ddCouponUsedMapper.deleteDdCouponUsedById(couponUsedId);
    }

    @Override
    public boolean uploadCouponUsed(String filePath)
    {
        String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
        String localFile = ddCouponUsedPath + File.separator + fileName;
//        String localFile = "G:\\1.xls";
        try
        {
            HttpClientUtil.downloadNetworkFile(filePath, localFile);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        List<String> lstColumns = new ArrayList<>();
        lstColumns.add("手机号");
        lstColumns.add("订单开始位置");
        lstColumns.add("订单结束位置");
        lstColumns.add("接单时间");
        lstColumns.add("结算费用");
        List<Map<String, String>> lstRowData = ExcelUtils.readExcelWithColumn(localFile, lstColumns);
        List<DdCouponUsed> lstCouponUsed = new ArrayList<>();
        Coupon coupon = new Coupon();
        List<Coupon> lstCopon = couponMapper.selectCouponList(coupon);
        for (Map<String, String> mRow : lstRowData)
        {
            DdCouponUsed ddCouponUsed = new DdCouponUsed();
            ddCouponUsed.setCustomerPhone(mRow.get("手机号"));
            ddCouponUsed.setArrival(mRow.get("订单结束位置"));
            ddCouponUsed.setDeparture(mRow.get("订单开始位置"));
            Date date;
            date = DateUtil.parseCST(mRow.get("接单时间"));
            ddCouponUsed.setUsedTime(date);
            ddCouponUsed.setCreateTime(DateUtils.getNowDate());
            ddCouponUsed.setUsedAmount(mRow.get("结算费用"));
            Customer customer = customerMapper.selectCustomerByContractPhoneOnes(ddCouponUsed.getCustomerPhone());
            if (customer != null)
            {
                //获取会员所有资产
                List<CustomerRightdetail> lstCustomerRight = customerRightdetailMapper.selectCustomerRightdetailByCustomerId(customer.getCustomerId());
                if (lstCustomerRight.size() > 0)
                {
                    for (CustomerRightdetail tmpCustomerRight : lstCustomerRight)
                    {
                        if (tmpCustomerRight.getCouponMoney() == null)
                        {
                            continue;
                        }
                        Coupon ddCoupon = lstCopon.stream().filter(item -> item.getCouponType().equals(CustomerConstants.CouponTypeDD.getValue())).findFirst().orElse(null);
                        if (ddCoupon != null && ddCoupon.getCouponId().equals(tmpCustomerRight.getRightdetailTypeid()) && tmpCustomerRight.getCouponMoney().compareTo(new BigDecimal(ddCouponUsed.getUsedAmount())) == 0 && tmpCustomerRight.getRightdetailState().equals(CustomerConstants.unexecuted.getValue()))
                        {
                            ddCouponUsed.setValidTime(tmpCustomerRight.getInvalidDate());
                            ddCouponUsed.setInvalidTime(tmpCustomerRight.getValidDate());
                            tmpCustomerRight.setRightdetailState(CustomerConstants.valid.getValue());
                            customerRightdetailMapper.updateCustomerRightdetail(tmpCustomerRight);
                            break;
                        }
                    }
                }
            }
            Long count = ddCouponUsedMapper.selectDdCouponUsedCount(ddCouponUsed);
            if (count == 0)
            {
                lstCouponUsed.add(ddCouponUsed);
            }
        }
        if (lstCouponUsed.size() == 0)
        {
            return true;
        }
        int result = ddCouponUsedMapper.insertBatchDdCouponUsed(lstCouponUsed);
        if (result > 0)
        {
            return true;
        }
        return false;
    }
}
